package com.wl.controller;

import com.wl.enums.CommentType;
import com.wl.exception.BusinessException;
import com.wl.model.Comment;
import com.wl.model.User;
import com.wl.service.CommentService;
import com.wl.service.NewsInfoService;
import com.wl.utils.PageUtil;
import com.wl.vo.NewsInfoVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import java.io.UnsupportedEncodingException;

import static com.wl.enums.CommentType.user;

/**
 * @author lb
 * @date 2018/4/17 18:01
 */
@RequestMapping("/comment")
@Controller
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    NewsInfoService newsInfoService;

    private final static Logger LOG = Logger.getLogger(CommentController.class);

    /***
     * 获取所用评论
     * */
    @GetMapping("/getAllComment")
    public ModelAndView getAllComment(PageUtil pageUtil, HttpServletRequest request) {
        PageUtil newPageUtil = this.getPageUtil(pageUtil);
        request.getSession().setAttribute("commentList", commentService.getLimitComment(newPageUtil));
        request.getSession().setAttribute("pageUtil", newPageUtil);
        final ModelAndView modelAndView = new ModelAndView("commentManage");
        return modelAndView;
    }

    /***
     * 修改评论
     * */
    @PostMapping("/updateComment")
    public ModelAndView updateComment(Comment comment, PageUtil pageUtil, HttpServletRequest request) {
        commentService.updateComment(comment);
        ModelAndView allComment = this.getAllComment(pageUtil, request);
        return allComment;
    }

    /***
     * 添加评论
     * */
    @PostMapping("/addComment")
    public ModelAndView addComment(Comment comment, PageUtil pageUtil, HttpServletRequest request) {
        final User user = (User) request.getSession().getAttribute("user");
        if(user == null){
           throw new BusinessException("用户未登录","");
        }
        commentService.add(comment.getContent(), CommentType.news,user.getUserId(),user.getUserId());
        ModelAndView allComment = this.getAllComment(pageUtil, request);
        return allComment;
    }

    /***
     * 添加评论
     * */
    @PostMapping("/news/addComment")
    public ModelAndView newsAddComment(Comment comment) {
        comment.setParentType("news");
        comment.setStatus("enable");
        commentService.addComment(comment);
        final ModelAndView modelAndView = new ModelAndView("newsDetail");
        final NewsInfoVo newsInfoVo = newsInfoService.newsInfoDetail(comment.getParentId());
        modelAndView.addObject("vo",newsInfoVo);
        return modelAndView;
    }

    @PostMapping("/findByComment")
    public ModelAndView findByComment(Comment comment, HttpServletRequest request) throws UnsupportedEncodingException {
        request.getSession().setAttribute("commentList", commentService.findByComment(comment));
        final ModelAndView modelAndView = new ModelAndView("commentManage");
        return modelAndView;
    }


    @GetMapping("/getComment")
    public ModelAndView getComment(long commentId, HttpServletRequest request) {
        request.getSession().setAttribute("comment", commentService.getComment(commentId));
        ModelAndView modelAndView = new ModelAndView("addAndUpdateComment");
        modelAndView.addObject("isUpdate","update");
        return modelAndView;
    }

    @GetMapping("/deleteComment")
    public ModelAndView deleteComment(long commentId, PageUtil pageUtil, HttpServletRequest request) {
        commentService.deleteComment(commentId);
        ModelAndView allComment = this.getAllComment(pageUtil, request);
        return allComment;
    }


    private PageUtil getPageUtil(PageUtil pageUtil) {
        pageUtil.getPageCount(commentService.getCountComment());
        if (pageUtil.getCurrentPage() == 0) {
            pageUtil.setCurrentPage(1);
        } else if (pageUtil.getCurrentPage() > pageUtil.getTotalPage()) {
            pageUtil.setCurrentPage(pageUtil.getCurrentPage() - 1);
        }
        pageUtil.setIndexPage((pageUtil.getCurrentPage() - 1) * pageUtil.getPageSize());
        return pageUtil;
    }


}
